<?php
/**
 * @package CAS Verify Email
 * @version 1
 */
/*
Plugin Name: CAS Verify Email
Description: This plugin to test whether an email address is a bu.edu address or not.
Author: J. Tyler Wiest
Version: 1
Author URI: http://www.jtwventures.com
License: GPLv2
*/

/*
To use this plugin plcae [verify] on www.mywebsite.com/verify
Everything else will be taken care of for you.
*/


// Register Short Code Verify
add_shortcode( 'verify', 'jtw_verify_email');

// The function that will replace [verify]
function jtw_verify_email( $post ) {
	
	// If form has been submitted Verify Email
	if(isset($_POST['email_ver'])) {
		// If the email passes verification go to survey
		if(verify_email(strip_tags($_POST['email_ver'])))
		{
			$page_title = $_REQUEST['p'];
			$base_url = $_REQUEST['b'];
			echo "Thank you please <a href='".$base_url.$page_title."-survey'>Proceed to Survey</a>";
		}
		// If the email does not pass verification then reshow form
		else {
			global $wp_query;
			$page_title = $_REQUEST['p'];
			$base_url = $_REQUEST['b'];
			echo "<strong>You must enter a valid BU.edu Email</strong><br/>";
			show_email_form($page_title, $base_url);
		}
	}
	// Else show form
	else {
		global $wp_query;
		if(isset($_REQUEST['p']) and isset($_REQUEST['b'])) {
			$page_title = $_REQUEST['p'];
			$base_url = $_REQUEST['b'];
			show_email_form($page_title, $base_url);
		}
		else {
			echo "I'm sorry but this page is not available please return <a href='/'>Home</a>";
		}
	}
}

function show_email_form($page_title, $base_url) {
	
	?>
	<form action="" method="post" class="email_verification" style="text-align: left;">
		<label for="email">Please enter you BU.edu Email Address:</label><br />
		<input type="text" name="email_ver" id="email" value=""/><br />
		<input type="submit" name="action" id="action-button" value="<?php echo("Verify Email") ?>"  /> 
		<input type="hidden" name="page_title" value="<?php echo $page_title ?>" />
		<input type="hidden" name="base_url" value="<?php echo $base_url ?>" />
	</form>
	<?php
}

function verify_email($email_ver) {
	return ( preg_match('/[a-z0-9-_]+@bu.edu$/', $email_ver) );
}

?>